#ifndef _CALLBACK_FACTORY_H_
#define _CALLBACK_FACTORY_H_

#include "num/named_set.h"
namespace num
{
/******
 * Used for mapping strings to class creation functions. Usually used for when you only know the parent and the text
 *	Assuming a Class Foo
 *		CallbackFactory<Foo>		FooFactory;
 * And a static creation function (note, must take in void*)
 *		static Foo*					CreateFoo(void* pData) { return new FooChild((FooResource*)pData)); }l
 * You'd then register it like this:
 *		FooFactory.Register("FooChild");
 * And use it like this
 *	Foo*	pNew = FooFactory.Create("FooChild", pData);
 */


template < class T >
class CallbackFactory
{
public:
	CallbackFactory() {};
	~CallbackFactory() {};

	typedef T*			(*FUNC)(void* pData);	
	struct	FunctionHolder
	{
		std::string	Name;
		FUNC		pFunc;
	};
	void		Register(const std::string& Name, FUNC pFunc)
	{
		if (!m_Callbacks.ElementExists(Name))
		{
			FunctionHolder* pData = new FunctionHolder();
			pData->pFunc = pFunc;
			pData->Name  = Name;
			m_Callbacks.AddElement(Name, pData);
		}
	}

	T*			Create(const std::string& Name, void* pPayload)
	{
		FunctionHolder* Holder = m_Callbacks.GetElement(Name);
		if (Holder && Holder->pFunc)
		{
			return Holder->pFunc(pPayload);
		}
		return NULL;
	}
private:
	num::NamedSet<FunctionHolder*>	m_Callbacks;
};
} // namespace num



#endif // _CALLBACK_FACTORY_H_